"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : standby_statement_history
Case Name   : guc参数:track_stmt_standby_chain_size,修改方式为alter system set,
              参数1,3修改为默认值,参数2,4为非法值,合理报错.
Create At   : 2022/10/13
Owner       : @li-xin12345
Description :
    1.查看当前track_stmt_standby_chain_size参数
    2.将参数2修改为<512的值,参数4为正常值,参数1,3修改为默认值
    3.将参数2修改为正常值,参数4为<512的值,参数1,3修改为默认值
    4.将参数2,4为<512的值,参数1,3修改为默认值
    5.将参数2修改为>1048576的值,参数4为正常值,参数1,3修改为默认值
    6.将参数2修改为正常值,参数4为>1048576的值,参数1,3修改为默认值
    7.将参数2,4修改为>1048576的值,参数1,3修改为默认值
    8.将参数2,4修改为字符,参数1,3修改为默认值
    9.恢复track_stmt_standby_chain_size参数默认值
Expect      :
    1.成功
    2.修改失败，合理报错
    3.修改失败，合理报错
    4.修改失败，合理报错
    5.修改失败，合理报错
    6.修改失败，合理报错
    7.修改失败，合理报错
    8.修改失败，合理报错
    9.恢复成功
History     :
"""
import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant

primary_sh = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == primary_sh.get_node_num(),
                 'Single node, and subsequent codes are not executed.')
class StandbyStatementHistory(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.standby_sh = CommonSH('Standby1DbUser')
        self.constant = Constant()
        self.common = Common('Standby1DbUser')
        self.chain_size_default = ''

    def test_standby_statement_history(self):
        text = "-----step1:查看当前track_stmt_standby_chain_size参数;expect:成功-----"
        self.logger.info(text)
        self.chain_size_default = self.common.show_param(
            'track_stmt_standby_chain_size')

        text = "-----step2:将参数2修改为<512的值,参数4为正常值,参数1,3修改为默认值;" \
               "expect:修改失败，合理报错-----"
        self.logger.info(text)
        alter_cmd = f"alter system set track_stmt_standby_chain_size " \
            f"to 32,256,16,1024;"
        alter_msg = self.standby_sh.execut_db_sql(alter_cmd)
        self.logger.info(alter_msg)
        self.assertIn(self.constant.invalid_value_msg, alter_msg, '执行失败' + text)

        text = "-----step3:将参数2修改为正常值,参数4为<512的值,参数1,3修改为默认值;" \
               "expect:修改失败，合理报错-----"
        self.logger.info(text)
        alter_cmd = f"alter system set track_stmt_standby_chain_size " \
            f"to 32,2048,16,256;"
        alter_msg = self.standby_sh.execut_db_sql(alter_cmd)
        self.logger.info(alter_msg)
        self.assertIn(self.constant.invalid_value_msg, alter_msg, '执行失败' + text)

        text = "-----step4:将参数2,4为<512的值,参数1,3修改为默认值;expect:修改失败，合理报错-----"
        self.logger.info(text)
        alter_cmd = f"alter system set track_stmt_standby_chain_size " \
            f"to 32,256,16,256;"
        alter_msg = self.standby_sh.execut_db_sql(alter_cmd)
        self.logger.info(alter_msg)
        self.assertIn(self.constant.invalid_value_msg, alter_msg, '执行失败' + text)

        text = "-----step5:将参数2修改为>1048576的值,参数4为正常值,参数1,3修改为默认值;" \
               "expect:修改失败，合理报错-----"
        self.logger.info(text)
        alter_cmd = f"alter system set track_stmt_standby_chain_size " \
            f"to 32,10485761,16,1024;"
        alter_msg = self.standby_sh.execut_db_sql(alter_cmd)
        self.logger.info(alter_msg)
        self.assertIn(self.constant.invalid_value_msg, alter_msg, '执行失败' + text)

        text = "-----step6:将参数2修改为正常值,参数4为>1048576的值,参数1,3修改为默认值;" \
               "expect:修改失败，合理报错-----"
        self.logger.info(text)
        alter_cmd = f"alter system set track_stmt_standby_chain_size " \
            f"to 32,512,16,10485761;"
        alter_msg = self.standby_sh.execut_db_sql(alter_cmd)
        self.logger.info(alter_msg)
        self.assertIn(self.constant.invalid_value_msg, alter_msg, '执行失败' + text)

        text = "-----step7:将参数2,4修改为>1048576的值,参数1,3修改为默认值;" \
               "expect:修改失败，合理报错-----"
        self.logger.info(text)
        alter_cmd = f"alter system set track_stmt_standby_chain_size " \
            f"to 32,10485761,16,10485761;"
        alter_msg = self.standby_sh.execut_db_sql(alter_cmd)
        self.logger.info(alter_msg)
        self.assertIn(self.constant.invalid_value_msg, alter_msg, '执行失败' + text)

        text = "-----step8:将参数2,4修改为字符,参数1,3修改为默认值;expect:修改失败，合理报错-----"
        self.logger.info(text)
        alter_cmd = f"alter system set track_stmt_standby_chain_size " \
            f"to 32,abc,16,DEF;"
        alter_msg = self.standby_sh.execut_db_sql(alter_cmd)
        self.logger.info(alter_msg)
        self.assertIn(self.constant.invalid_value_msg, alter_msg, '执行失败' + text)

    def tearDown(self):
        text = "-----step9:恢复track_stmt_standby_chain_size参数默认值;" \
               "expect:恢复成功-----"
        self.logger.info(text)
        alter_cmd = f"alter system set track_stmt_standby_chain_size " \
            f"to 32, 1024, 16, 512;"
        alter_msg = self.standby_sh.execut_db_sql(alter_cmd)
        self.logger.info(alter_msg)
        show_msg = self.common.show_param('track_stmt_standby_chain_size')
        self.assertIn(self.constant.alter_system_success_msg,
                      alter_msg, '执行失败' + text)
        self.assertIn('32, 1024, 16, 512', show_msg, '执行失败' + text)
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
